package com.xy.demo.Util;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("unchecked")
public class Data implements Serializable {

	private static final long serialVersionUID = 1L;
	private Map<Object, Object> ext;

	public void setExt(Map<Object, Object> ext) {
		this.ext = ext;
	}

	public Data() {
	}
	
	public boolean containsKey(Object key) {
		if (ext == null) {
			return false;
		}
		if (ext.containsKey(key)) {
			return true;
		}
		return false;
	}
	
	public void put(Object key, Object value) {
		if (ext == null) {
			ext = new HashMap<Object, Object>();
		}
		ext.put(key, value);
	}
	
	public <T> T get(Object key) {
		if (ext == null) {
			return null;
		}
		if (ext.containsKey(key)) {
			return (T) ext.get(key);
		}
		return null;
	}
	
	
	public void setModel(Object key, Object value) {
		if (ext == null) {
			ext = new HashMap<Object, Object>();
		}
		ext.put(key, value);
	}
	public <T> T getModel(Object key) {
		if (ext == null) {
			return null;
		}
		if (ext.containsKey(key)) {
			return (T) ext.get(key);
		}
		return null;
	}
	
	public Object getex(Object key) {
		if (ext == null) {
			return null;
		}
		if (ext.containsKey(key)) {
			return ext.get(key);
		}
		return null;
	}
	
	public <T> void setList(Object key, List<T> list) {
		if (ext == null) {
			ext = new HashMap<Object, Object>();
		}
		ext.put(key, list);
	}
	public <T> void setList(List<T> list) {
		setList("list", list);
	}
	
	public <T> List<T> getList(Object key) {
		if (ext == null) {
			return null;
		}
		if (ext.containsKey(key)) {
			return (List<T>) ext.get(key);
		}
		return null;
	}
	public <T> List<T> getList() {
		return getList("list");
	}
	
	public Map<Object, Object> getExt() {
		return ext;
	}

	public String toStringExt() {
		if (ext == null) {
			return "";
		}
		Iterator<Map.Entry<Object, Object>> es = ext.entrySet().iterator();
		Map.Entry<Object, Object> e = null;
		Object key = null;
		Object value = null;

		StringBuffer sb = new StringBuffer(0);
		sb.append("Ext: ");
		while (es.hasNext()) {
			e = es.next();
			if (e.getValue() == null) {
				continue;
			}

			key = e.getKey();
			value = e.getValue();

			sb.append(key + "=" + value);
		}
		return sb.toString();
	}
	
}
